package com.hymall.service.wx;

import com.hymall.bean.*;
import com.hymall.bean.vo.WxHomeIndexInnerFloorGoodsVO;
import com.hymall.bean.vo.WxHomeIndexVO;
import com.hymall.mapper.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: 章鹏
 * @description 小程序首页
 * @date: 2022/7/19 15:33
 */
@Service
public class WxHomeServiceImpl implements WxHomeService {
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Autowired
    MarketAdMapper marketAdMapper;

    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Autowired
    MarketSystemMapper marketSystemMapper;

    @Autowired
    AdminConfigMapper adminConfigMapper;

    @Override
    public WxHomeIndexVO getHomeInfo() {
        WxHomeIndexVO wxHomeIndexVO = new WxHomeIndexVO();

        //获取newGoodsList
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        marketGoodsExample.createCriteria().andIsNewEqualTo(true);
        List<MarketGoods> newGoodsList = marketGoodsMapper.selectByExample(marketGoodsExample, "desc");
        getLimitedList(newGoodsList, "market_wx_index_new");
        wxHomeIndexVO.setNewGoodsList(newGoodsList);

        //获得couponList
        MarketCouponExample couponExample = new MarketCouponExample();
        couponExample.createCriteria().andDeletedEqualTo(false).andStatusEqualTo((short) 0).andTypeEqualTo((short) 0);
        List<MarketCoupon> couponList = marketCouponMapper.selectByExample(couponExample);
        wxHomeIndexVO.setCouponList(couponList);

        // 获取Banner
        MarketAdExample adExample = new MarketAdExample();
        adExample.createCriteria().andDeletedEqualTo(false).andEnabledEqualTo(true);
        List<MarketAd> banner = marketAdMapper.selectByExample(adExample);
        wxHomeIndexVO.setBanner(banner);

        //获取brandList
        MarketBrandExample brandExample = new MarketBrandExample();
        brandExample.createCriteria().andDeletedEqualTo(false);
        List<MarketBrand> brandList = marketBrandMapper.selectByExample(brandExample);
        getLimitedList(brandList, "market_wx_index_brand");
        wxHomeIndexVO.setBrandList(brandList);

        // 获取hotGoodsList
        MarketGoodsExample hotGoodsExample = new MarketGoodsExample();
        hotGoodsExample.createCriteria().andDeletedEqualTo(false).andIsHotEqualTo(true);
        List<MarketGoods> hotGoodsList = marketGoodsMapper.selectByExample(hotGoodsExample, "desc");
        getLimitedList(hotGoodsList, "market_wx_index_hot");
        wxHomeIndexVO.setHotGoodsList(hotGoodsList);

        // 获取topicList
        MarketTopicExample topicExample = new MarketTopicExample();
        topicExample.createCriteria().andDeletedEqualTo(false);
        List<MarketTopic> topicList = marketTopicMapper.selectByExample(topicExample);
        getLimitedList(topicList, "market_wx_index_topic");
        wxHomeIndexVO.setTopicList(topicList);

        //获取channel
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.createCriteria().andIdIsNotNull();
        List<MarketCategory> marketCategoryList = marketCategoryMapper.selectByExample(marketCategoryExample);
        getLimitedList(marketCategoryList,"market_wx_catlog_list");
        wxHomeIndexVO.setChannel(marketCategoryList);

        //获取底部品类商品信息floorGoodsList
        List<WxHomeIndexInnerFloorGoodsVO> floorGoodsList = new ArrayList<>();
        for (MarketCategory marketCategory : marketCategoryList) {
            WxHomeIndexInnerFloorGoodsVO wxHomeIndexInnerFloorGoodsVO = new WxHomeIndexInnerFloorGoodsVO();
            Integer categoryId = marketCategory.getId();
            try {
                BeanUtils.copyProperties(wxHomeIndexInnerFloorGoodsVO, marketCategory);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            MarketGoodsExample goodsExample = new MarketGoodsExample();
            goodsExample.createCriteria().andCategoryIdEqualTo(categoryId);
            List<MarketGoods> marketGoodsList = marketGoodsMapper.selectByExample(goodsExample, "desc");
            getLimitedList(marketGoodsList, "market_wx_catlog_goods");
            wxHomeIndexInnerFloorGoodsVO.setGoodsList(marketGoodsList);
            floorGoodsList.add(wxHomeIndexInnerFloorGoodsVO);
        }
        getLimitedList(floorGoodsList, "market_wx_catlog_list");
        wxHomeIndexVO.setFloorGoodsList(floorGoodsList);
        return wxHomeIndexVO;
    }

    //获得最终应该传入的List
    private void getLimitedList(List newGoodsList, String limitKeyName) {
        String limit = adminConfigMapper.queryMarketMallConfigValue(limitKeyName);
        Integer market_wx_index_new = Integer.parseInt(limit);
        if (market_wx_index_new >= newGoodsList.size()) {
            return;
        }
        for (int i = market_wx_index_new; i < newGoodsList.size(); ) {
            newGoodsList.remove(i);
        }
    }


}
